import { Component, ViewChild ,ChangeDetectorRef} from '@angular/core';
import { IonSlides, IonInfiniteScroll, ActionSheetController } from '@ionic/angular';
import { HttpService } from '../../services/http.service';
import { Router } from "@angular/router";
import { LoginInfoService } from '../.././services/login-info.service';
import { BaseUiService } from '../../services/base-ui.service';
import {EventService } from '../../services/QaService';
import { commService } from '../../services/comm.service';
@Component({
  selector: 'app-tab3',
  templateUrl: 'inquire-page.html',
  styleUrls: ['inquire-page.scss']
})

export class InquirePage {
  @ViewChild(IonSlides, { static: true }) slides: IonSlides;
  @ViewChild(IonInfiniteScroll, { static: true }) infiniteScroll: IonInfiniteScroll;
  public tabsNum:number = 0
  public userId:any //当前登录的人的idCard
  public dataLength:number //数据的长度
  public data:any = [] //订单数据
  public pageSize:number = 10 //每次请求数据的多少
  public notaryForm:number = 2 //公证类型 1 在线 2 视频 3 三方
  public total:any //数据库的所有数据的数量
  public data1=[];
  public data2=[];
  slideOpts = {
    initialSlide: 0,
    autoHeight: true
  };
  constructor(
    public actionSheetController: ActionSheetController,
    public commService: commService,
    public baseUiService:BaseUiService,
    public httpService: HttpService,
    public router: Router,
    private cd: ChangeDetectorRef,
    public eventService:EventService,
    public LoginInfoService: LoginInfoService) { }
  ngOnInit(){
    this.eventService.eventEmitter.on('loginEvent',()=>{
      this.userId = this.LoginInfoService.getInfo().idCard || "";
      this.search()
    })
    this.userId = this.LoginInfoService.getInfo().idCard || "";
    this.search()
  }

 
  search() {
    // this.dataLength = 1
    console.log("-------search-------",this.LoginInfoService.getInfo().idCard);
    console.log(this.userId == undefined)
    if(!!this.userId ){
      let params = {
        idCard: this.LoginInfoService.getInfo().idCard,
        currentPage:1,
        pageSize:this.pageSize,
        notaryForm:this.notaryForm,
      }
      this.cd.detectChanges();
      this.httpService.notarylist(params).then(
        res => {
          // console.log(res, 1212)
          if(res['code'] == 200){
            res["items"].forEach(e => {
              let arr = []
              arr = e.notaryItemNames.split('/')
              if(arr.length > 1){
                e.notaryItemNames = arr[0]+'等'+arr.length +'项'
              }else{
                e.notaryItemNames = arr[0]
              }
            });
            this.total = res['page'].total
            if(this.notaryForm==1){
              this.data2 = res["items"];
            }else{
              this.data1 = res["items"];
            }
            this.cd.detectChanges();
          }
        }
      )
    }
  }
  //点击tab切换
  segmentChange(e:any) {
    console.log("--------------segmentChange-----------")
    this.tabsNum = e;
    this.slides.slideTo(e, 500)
    if(e == 0){
      this.notaryForm = 2
    }else{
      this.notaryForm = 1
    }
    setTimeout(()=>{
      this.cd.detectChanges();
    },200)
    this.search()

  }
  //手势切换时
  sildeChange() {
    console.log("----------sildeChange----------------")
    this.cd.detectChanges();
    this.slides.getActiveIndex().then(index => {
      if(index == 0){
        this.notaryForm = 2
      }else{
        this.notaryForm = 1
      }
      this.search()
      this.tabsNum = index
    
    })
   
  }
  
  //下拉刷新
  doRefresh(event) {
    console.log("----------doRefresh----------------")
    setTimeout(() => {
      event.target.complete();//不加会卡死
      this.search()
    }, 1000);
    setTimeout(()=>{
      this.cd.detectChanges();
    },200)
  }
  //上拉加载
  loadData(event) {
    setTimeout(() => {
      console.log('Done');
      event.target.complete();//不加会卡死
      if (this.data.length == this.total) {
        event.target.disabled = true;
      }
      this.pageSize += 10
      this.search()
    }, 500);
    setTimeout(()=>{
      this.cd.detectChanges();
    },200)
  }
  //去订单详情
  goOrder(num:number,info:any){
    console.log(info)

    this.router.navigate(['/inquire/order'], { queryParams: {
      orderId:info,
      isleixing:num
    } })
  }

  //去付款
  doVideologSetState(info:any){
    let param={
      outTradeNo: info 
    }
    this.httpService.doSign(param).then(res=>{  
      if(res['code']=="200"){
        this.commService.aliPay(res['item']).subscribe(res=>{
          console.log("---------------支付-------------------------------------")
          console.log(res)
          if(res.trim() =="5001"){
              this.baseUiService.showToast('支付失败','customblue','top');
            }else{
              this.search();
            }
        })
      }
    })
  }

  async makeUp(info:any){
    const actionSheet = await this.actionSheetController.create({
      header: '支付',
      cssClass:"relay",
      buttons: [{
        text: '支付宝支付',
        role: 'share',
        icon: 'iconfont icon-zhifubao1',
        handler: () => {
          this.doVideologSetState(info + '-2');
        }
      },{
        text: '关闭',
        icon: 'close',
        role: 'cancel',
        handler: () => {
        }
      }]
    });
    await actionSheet.present();
  }

  //付款弹出
  async replay(info:any) {
    const actionSheet = await this.actionSheetController.create({
      header: '支付',
      cssClass:"relay",
      buttons: [{
        text: '支付宝支付',
        role: 'share',
        icon: 'iconfont icon-zhifubao1',
        handler: () => {
          this.doVideologSetState(info);
        }
      },{
        text: '关闭',
        icon: 'close',
        role: 'cancel',
        handler: () => {
        }
      }]
    });
    await actionSheet.present();
  }

}
